//
//  CYCitysPickerView.m
//  CreditAddressBook
//
//  Created by Lee on 15/6/3.
//  Copyright (c) 2015年 Lee. All rights reserved.
//

#import "CYCitysPickerView.h"
#import "MJExtension.h"
#import "CYCities.h"
#import "NSObject+XML.h"
#import "LEProvinceXML.h"

@interface CYCitysPickerView () <UIPickerViewDataSource, UIPickerViewDelegate>
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;

@property (nonatomic, strong) NSArray *provinces;//省份
@property (nonatomic, strong) NSArray *cities;//城市
@property (nonatomic, strong) NSArray *areas;//区
@property (nonatomic, strong) NSString *selectedProvince;//选中的省份
@property (nonatomic, strong) NSString *selectedCity;//选中的城市
@property (nonatomic, strong) NSString *selectedareas;//选中的区
@end

@implementation CYCitysPickerView

- (instancetype)initWithPoint:(CGPoint)point delegate:(id)delegate {
    self = [[[NSBundle mainBundle] loadNibNamed:@"CYCitysPickerView" owner:nil options:nil] firstObject];
    if (self) {
        self.frame = (CGRect){point, CGSizeMake([UIScreen mainScreen].bounds.size.width, 0)};
//        self.provinces = [CYCities objectArrayWithFilename:@"city.plist"];
        LEProvinceXML *province = [LEProvinceXML objectWithXMLFilename:@"area.xml"];
        self.provinces = province.province;
//        CYCities *province = [self.provinces firstObject];
//        self.cities = province.cities;
        LEAreaXML *area = self.provinces.firstObject;
        self.cities = area.city;
//        LECountryXML *country  =  [[LECountryXML alloc]init];
        
        self.pickerDelegate = delegate;
        self.pickerView.delegate = self;
        self.pickerView.dataSource = self;
    }
    return self;
}

#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2;
}


- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    switch (component) {
        case 0:
            return self.provinces.count;
            break;
            
        default:
            return self.cities.count;
            break;
    }
}

#pragma mark - UIPickerViewDelegate
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0) {
//        CYCities *province = self.provinces[row];
        LEAreaXML *area = self.provinces[row];
//        self.cities = province.cities;
        self.cities = area.city;
        [self.pickerView reloadComponent:1];
        
        [self.pickerView selectRow:0 inComponent:1 animated:YES];
        
//        self.selectedProvince = province.state;
//        self.selectedCity = [province.cities firstObject];//默认选中第一个城市
        self.selectedProvince = area._name;
        LECountryXML *country = area.city.firstObject;
        self.selectedCity = country._name;
    } else {
        LECountryXML *county = self.cities[row];
//        self.selectedCity = self.cities[row];
        self.selectedCity = county._name;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (component == 0) {
//        CYCities *province = self.provinces[row];
//        return province.state;
        LEAreaXML *area = self.provinces[row];
        return area._name;
    } else {
        LECountryXML *county = self.cities[row];
        return county._name;
//        return self.cities[row];
    }
}

- (IBAction)confirmBtn:(id)sender {
    if ([self.pickerDelegate respondsToSelector:@selector(pickerView:selectedProvince:city:)]) {
        
        [self.pickerDelegate pickerView:self selectedProvince:self.selectedProvince city:self.selectedCity];
    }
    
}

- (void)setFrame:(CGRect)frame {
    frame.size.height = 216;
    [super setFrame:frame];
}
@end
